﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace Esint.Common.Data.SqlDAL
{
    public class SQLLogInfo:SysSqllogInfo
    {
        /// <summary>
        /// 插入数据
        /// </summary>
        public void End(SqlTransaction trans)
        {
            this.Endtime = DateTime.Now;
            this.Timespan = (this.Endtime.Value - this.Starttime.Value).TotalMilliseconds;

            this.Logid = Guid.NewGuid();

            string sqlText = "INSERT INTO T_SYS_SQLLOG (LOGID,SQLTEXT,STARTTIME,ENDTIME,TIMESPAN) values(@LOGID,@SQLTEXT,@STARTTIME,@ENDTIME,@TIMESPAN)";

            SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sqlText, BuilderParameters());
        }

        /// <summary>
        /// 插入数据
        /// </summary>
        public void End(SqlConnection conn)
        {
            this.Endtime = DateTime.Now;
            this.Timespan = (this.Endtime.Value - this.Starttime.Value).TotalMilliseconds;

            this.Logid = Guid.NewGuid();

            string sqlText = "INSERT INTO T_SYS_SQLLOG (LOGID,SQLTEXT,STARTTIME,ENDTIME,TIMESPAN) values(@LOGID,@SQLTEXT,@STARTTIME,@ENDTIME,@TIMESPAN)";

            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlText, BuilderParameters());
        }


        private SqlParameter[] BuilderParameters()
        {
            SqlParameter[] parameters = new SqlParameter[5];
            parameters[0] = new SqlParameter();
            parameters[0].ParameterName = "@LOGID";
            parameters[0].Value = this.Logid;
            parameters[0].SqlDbType = SqlDbType.UniqueIdentifier;
            parameters[0].Direction = ParameterDirection.Input;

            parameters[1] = new SqlParameter();
            parameters[1].ParameterName = "@SQLTEXT";
            parameters[1].Value = this.Sqltext;
            parameters[1].SqlDbType = SqlDbType.NText;
            parameters[1].Direction = ParameterDirection.Input;

            parameters[2] = new SqlParameter();
            parameters[2].ParameterName = "@STARTTIME";
            parameters[2].Value = this.Starttime;
            parameters[2].SqlDbType = SqlDbType.DateTime;
            parameters[2].Direction = ParameterDirection.Input;

            parameters[3] = new SqlParameter();
            parameters[3].ParameterName = "@ENDTIME";
            parameters[3].Value = this.Endtime;
            parameters[3].SqlDbType = SqlDbType.DateTime;
            parameters[3].Direction = ParameterDirection.Input;

            parameters[4] = new SqlParameter();
            parameters[4].ParameterName = "@TIMESPAN";
            parameters[4].Value = this.Timespan;
            parameters[4].SqlDbType = SqlDbType.Decimal;
            parameters[4].Direction = ParameterDirection.Input;
            return parameters;
        }
    }
}
